functionFastClick(layer, options) { // ... var methods = ['onMouse', 'onClick', 'onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel']; var context = this; for (var i = 0, l = methods.length; i < l; i++) { context[methods[i]] = bind(context[methods[i]], context); }
// Set up event handlers as required if (deviceIsAndroid) { layer.addEventListener('mouseover', this.onMouse, true); layer.addEventListener('mousedown', this.onMouse, true); layer.addEventListener('mouseup', this.onMouse, true); }
// Don't send a synthetic click to disabled inputs (issue #62) case'button': case'select': case'textarea': if (target.disabled) { returntrue; }
break; case'input':
// File inputs need real clicks on iOS 6 due to a browser bug (issue #68) if ((deviceIsIOS && target.type === 'file') || target.disabled) { returntrue; }
break; case'label': case'iframe': // iOS8 homescreen apps can prevent events bubbling into frames case'video': returntrue; }
// If a target element was never set (because a touch event was never fired) allow the event if (!this.targetElement) { returntrue; }
if (event.forwardedTouchEvent) { returntrue; }
// Programmatically generated events targeting a specific element should be permitted if (!event.cancelable) { returntrue; }
// Derive and check the target element to see whether the mouse event needs to be permitted; // unless explicitly enabled, prevent non-touch click events from triggering actions, // to prevent ghost/doubleclicks. if (!this.needsClick(this.targetElement) || this.cancelNextClick) {
// Prevent any user-added listeners declared on FastClick element from being fired. // 阻止事件冒泡并且阻止相同事件的其他侦听器被调用 if (event.stopImmediatePropagation) { event.stopImmediatePropagation(); } else {
// Part of the hack for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2) event.propagationStopped = true; }
// Cancel the event event.stopPropagation(); event.preventDefault();
returnfalse; }
// If the mouse event is permitted, return true for the action to go through. returntrue; };
FastClick.prototype.onClick = function(event) { // ... permitted = this.onMouse(event); // ... // Only unset targetElement if the click is not permitted. This will ensure that the check for !targetElement in onMouse fails and the browser's click doesn't go through. // 若不允许此次 click 事件,则重置 this.targetElement if (!permitted) { this.targetElement = null; }
// If clicks are permitted, return true for the action to go through. return permitted; };